package pac;

 /*public class Mylink {
    static class Link
    {
        public int shu;
        public Link next;


        //public

        public Link(int shu) {
            this.shu = shu;
        }
    }
    public  Link head;
    public  void creat() {
        Link a = new Link(20);
        Link b=new Link(30);
        Link c=new Link(40);
        Link d=new Link(50);
        Link f=new Link(60);
        a.next=b;
        b.next=c;
        c.next=d;
        d.next=f;
        //d.next=null;
        this.head=a;




    }

    //public static Link val=this.head;
    public  int size()
    {
        Link val=this.head;
        int count=0;

        while(val!=null)
        {
            count++;
            val=val.next;


        }
        return count;
    }
    public  void dispaly(Link c)
    {
        Link val=c;
        while(val!=null)
        {
            System.out.print(val.shu+" ");
            val=val.next;
        }
        System.out.println();
    }
    public boolean contains(int date)
    {
        Link val=this.head;
        while(val!=null)
        {
            if(val.shu==date)
            {
                return true;
            }
            val=val.next;
        }
        return false;

    }


    public void add1(int i,int data)
    {
        if(i<0||i>size())
        {
            System.out.println("不合法");
        }

        if(i==0)
        {
            addtou(i);
        }
        else if(i==size())
        {
            addwei(i);
        }
        else
        {
            //Link c=this.head;
            Link d=find(i);
            Link f=new Link(data);
            f.next=d.next;
            d.next=f;







        }
    }
    public Link find(int data)
    {

        Link c=this.head;
        while(data-1!=0)
        {
            c=c.next;
            data--;
        }
        return c;

    }

    public void clear()
    {
        this.head=null;
    }

    public void addtou(int data)
    {
        Link g=new Link(data);
        if(this.head==null)
        {
            this.head=g;
        }
        else {
            g.next=this.head;
            this.head=g;
        }







    }
    public void addwei(int data)
    {

        //Link val=this.head;
        Link g=new Link(data);
        if(this.head==null)
        {
            this.head=g;

        }
        else {
            Link val = this.head;
            while (val.next != null) {
                val = val.next;
            }
            val.next = g;
        }

        //g.next=null;

    }
    public void remove(int key)
    {
        Link c=this.head;
        if(this.head==null)
        {
            System.out.println("不存在要删除的元素");
            return;
        }
        else if(this.head.shu==key)
        {
            this.head=this.head.next;
            return;
        }
        else
        {
            Link d=findkey(key);
            Link f=d.next;
            d.next=f.next;
        }


    }
    public Link findkey(int key)
    {
        Link c=this.head;
        while(c.next!=null)
        {
            if(c.next.shu==key)
            {
                return c;

            }
            c=c.next;


        }
        return null;
    }
     public static Link  partition(Link pHead, int x) {

         Link a=null;
         Link b=null;
         Link c=null;
         Link d=null;
         Link e=pHead;
         while(e!=null)
         {
             if(e.shu<x)
             {
                 if(a==null)
                 {
                     a=e;
                     b=e;
                 }
                 else
                 {
                     b.next=e;
                     b=e;

                 }

             }
             else
             {
                 if(c==null)
                 {
                     c=e;
                     d=e;
                 }
                 else
                 {
                     d.next=e;
                     d=e;

                 }


             }
             e=e.next;
         }


         if(a==null&&c!=null)
         {
             return c;
         }
         b.next=c;
         d.next=null;
         return a;












         // write code here
     }











     public void removeall(int key)
    {
        if(this.head==null)
        {
            return;
        }

        while(this.head.shu==key)
        {
            this.head=this.head.next;



        }
        Link slow=this.head;
        Link fast=this.head.next;
        while(fast!=null)
        {
            if(fast.shu==key)
            {
               slow.next=fast.next;
               fast=fast.next;
            }
            else
            {
                slow=fast;
                fast=fast.next;

            }

        }
    }
    public void reverse()
    {
        Link d=this.head;
        while(d.next!=null)
        {
            d=d.next;
        }
        this.head=d;
        Link c=this.head;
        while(c.next!=null)
        {
            c.next=c.next;

        }
    }
     public Link reverseList() {
         if(this.head==null)
         {
             return null;
         }
         if(this.head.next==null)
         {
             return head;
         }
         Link c=this.head.next;
         this.head.next=null;
         while(c!=null)
         {


             c.next=this.head;
             this.head=c;
             Link d=c.next;

             c=d;

         }
         return this.head;


     }
     public  Link Find(int k)
     {
         if(k<=0)
         {
             System.out.println("k不合法");
             return null;
         }
         if(this.head==null)
         {
             return null;
         }

        Link fast=this.head;
        Link slow=this.head;
         while(k-1!=0)
         {
             fast=fast.next;
             if(fast==null)
             {
                 return null;
             }
             k--;
         }
         while(fast.next!=null)
         {
             fast=fast.next;
             slow=slow.next;
         }
         return slow;
     }
     ublic static Link removeNthFromEnd (Link head, int n) {
         Link c=head;
         int count=0;
         while(c!=null)
         {
             count++;
             c=c.next;

         }
         if(count==n)
         {
             head=head.next;
             return head;
         }
         Link fast=head;
         Link slow=head;
         while(n-1!=0)
         {
             fast=fast.next;
             n--;
         }
         while(fast.next.next!=null)
         {
             fast=fast.next;
             slow=slow.next;
         }
         slow.next=slow.next.next;
         return head;


         // write code here
     }


     public static Link deleteDuplicates (Link head) {
         if(head==null)
         {
             return null;
         }
         if(head.next==null)
         {
             return head;
         }
         Link pre=head;
         Link cur=head.next;
         while(cur!=null)
         {
             if(cur.shu==pre.shu)
             {
                 cur=cur.next;

             }
             else
             {
                 pre.next=cur;
                 pre=cur;

                 cur=pre.next;
             }
         }
         pre.next=null;
         return head;




         // write code here
     }
     public void addFirst(int data)
     {
         ListNode a=new ListNode(data);
         if(head==null)
         {
             head=a;

         }
         else
         {
             ListNode b=tail;
             a.next=head;
             while(b.next!=null)
             {
                 b=b.next;
             }
             b.next=a;
             a.next=null;
             head=a;



         }
         return head;
         public void addindex(int index,int data)
         {
             if(index<=0||index>size())
             {
                 return;
             }
             if(index==1)
             {
                 addtou(data);
             }
             if(index==size())
             {
                 addwei(data);
             }
             ListNode cur=find(intdex);
             ListNode a=cur.prev;
             node.next=cur;
             cue.prev=node;
             a.next=node;
             node.prev=a;

         }

     }
     public void clear()
     {
         ListNode b=head;

         while(b!=null)
         {
             ListNode c=b.next;
             b.next=null;
             b.prev=null;
             b=c;

         }
     }

 }














class text
{




    public static void main(String[] args) {
        Mylink d=new Mylink();
        //d.creat();
        d.addwei(8);
        d.addwei(1);
       d.addwei(8);
       d.addwei(8);
      //d.addwei(5);
      //d.addwei(5);
       // d.addwei(6);
       //Mylink.Link f= Mylink.removeNthFromEnd(d.head,2);
        Mylink.Link f= Mylink.deleteDuplicates (d.head);




       d.dispaly(f);


       //d.remove(1);
       //d.dispaly(d.head);
        //d.add1(1,99);
        //d.dispaly(d.head);
       /* Mylink g=new Mylink();
        g.addwei(1);
        g.addwei(7);
        g.addwei(9);
        g.addwei(10);
        g.addwei(11);
        g.addwei(12);
        Mylink.Link l=paixu(d.head,g.head);
        //System.out.println(l);
        //d.add1(1,99);
        d.dispaly(l);



        System.out.println(d.contains(4));
        d.addtou(99);
        d.addtou(100);

        d.add1(1,99);
        d.dispaly();

        System.out.println(d.find(2));
        d.remove(100);
        d.dispaly();

        d.dispaly();
        d.remove(0);
        d.dispaly();
        d.add1(1,4);
        d.dispaly();
        d.remove(5);
        d.dispaly();
        d.removeall(99);
        d.dispaly();
        d.removeall(100);
        d.dispaly();
        d.reverseList();
        d.dispaly();
        Mylink.Link c=d.Find(1);
       System.out.println(c.shu);

  */




















